package com.web.base.log.util;

import com.blue.jdbc.Page;
import com.web.base.log.message.MessageRequest;
import com.web.base.log.message.MessageResponse;
import com.web.base.log.model.Message;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月20日
 */
public class MessageConvertor
{
	private MessageConvertor()
	{
	}
	
	public static Message toMessage(MessageRequest request)
	{
		if (request == null)
			return null;
		
		Message message = new Message();
		message.setId(request.getId());
		message.setSenderId(request.getSenderId());
		message.setReceiverId(request.getReceiverId());
		message.setTitle(request.getTitle());
		message.setContent(request.getContent());
		message.setCreateTime(request.getCreateTime());
		message.setReadTime(request.getReadTime());
		message.setRead(request.getRead());
		return message;
	}
	
	public static List<Message> toMessage(List<MessageRequest> requestList)
	{
		List<Message> list = new ArrayList<>();
		if (requestList == null || requestList.isEmpty())
			return list;
		
		for (MessageRequest request : requestList)
		{
			list.add(toMessage(request));
		}
		return list;
	}
	
	public static MessageResponse toResponse(Message message)
	{
		if (message == null)
			return null;
		
		MessageResponse response = new MessageResponse();
		response.setId(message.getId());
		response.setSenderId(message.getSenderId());
		response.setReceiverId(message.getReceiverId());
		response.setTitle(message.getTitle());
		response.setContent(message.getContent());
		response.setCreateTime(message.getCreateTime());
		response.setReadTime(message.getReadTime());
		response.setRead(message.getRead());
		return response;
	}
	
	public static List<MessageResponse> toResponse(List<Message> list)
	{
		List<MessageResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (Message message : list)
		{
			responseList.add(toResponse(message));
		}
		return responseList;
	}
	
	public static Map<Integer, MessageResponse> toResponse(Map<Integer, Message> map)
	{
		Map<Integer, MessageResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, Message> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue()));
		}
		return responseMap;
	}
	
	public static Page<MessageResponse> toResponse(Page<Message> page)
	{
		if (page == null)
			return null;
		
		Page<MessageResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList()));
		return responsePage;
	}
	
}
